import { useStore } from '@renderer/store/useStore'
import { ChangeEvent } from 'react'

export default () => {
  // zustand
  const setData = useStore((state) => state.setData)
  const search = useStore((state) => state.search)
  const setSearch = useStore((state) => state.setSearch)

  const handleSearch = async (e: ChangeEvent<HTMLInputElement>) => {
    setSearch(e.target.value)
    if (e.target.value === '') {
      setData([])
    } else {
      const data: ContentType[] = await window.api.sql(
        `select * from contents where title like @title limit 7`,
        'findAll',
        { title: `%${e.target.value}%` }
      )
      console.log(data, 21)

      setData(data)

      // setData(
      //   codes
      //     .filter((code) => code.content.toLowerCase().includes(e.target.value.toLowerCase()))
      //     .slice(0, 7)
      // )
    }
  }

  return { search, handleSearch }
}
